﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using eShop.DataAccess.Entity;

namespace eShop.DataAccess
{
    public partial class ShippingRateDAL : BaseDAL
    {
        public static bool Insert(ShippingRate shippingRate)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("ShippingRate_InsertOrUpdate");
            db.AddOutParameter(dbCommand, "Id", DbType.Int32, 4);
            db.AddInParameter(dbCommand, "CityId", DbType.Int32, shippingRate.CityId);
            db.AddInParameter(dbCommand, "StartingWeight", DbType.Double, shippingRate.StartingWeight);
            db.AddInParameter(dbCommand, "EndingWeight", DbType.Double, shippingRate.EndingWeight);
            db.AddInParameter(dbCommand, "Rate", DbType.Double, shippingRate.Rate);

            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                shippingRate.Id = Convert.ToInt32(db.GetParameterValue(dbCommand, "@Id"));
                return true;
            }
            return false;
        }

        public static bool Update(ShippingRate shippingRate)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("ShippingRate_InsertOrUpdate");
            db.AddInParameter(dbCommand, "Id", DbType.Int32, shippingRate.Id);
            db.AddInParameter(dbCommand, "CityId", DbType.Int32, shippingRate.CityId);
            db.AddInParameter(dbCommand, "StartingWeight", DbType.Double, shippingRate.StartingWeight);
            db.AddInParameter(dbCommand, "EndingWeight", DbType.Double, shippingRate.EndingWeight);
            db.AddInParameter(dbCommand, "Rate", DbType.Double, shippingRate.Rate);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        public static List<ShippingRate> GetAll(Boolean allowPaging = false, Int32 pageIndex = 0, Int32 pageSize = 0, String orderColumn = "Id", Boolean isAscending = false)
        {
            List<ShippingRate> shippingRateList = new List<ShippingRate>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("ShippingRate_GetAll");
            db.AddInParameter(dbCommand, "AllowPaging", DbType.Boolean, allowPaging);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbCommand, "OrderColumn", DbType.String, orderColumn);
            db.AddInParameter(dbCommand, "IsAscending", DbType.Boolean, isAscending);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                shippingRateList = GetEntitiesFromReader(dataReader);
            }
            return shippingRateList;
        }

        public static List<ShippingRate> GetByKeys(Int32? id, Int32? cityId)
        {
            List<ShippingRate> shippingRateList = new List<ShippingRate>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("ShippingRate_GetByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (cityId.HasValue)
                db.AddInParameter(dbCommand, "CityId", DbType.Int32, cityId);

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                shippingRateList = GetEntitiesFromReader(dataReader);
            }
            return shippingRateList;
        }

        public static bool DeleteByKeys(Int32? id, Int32? cityId)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("ShippingRate_DeleteByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (cityId.HasValue)
                db.AddInParameter(dbCommand, "CityId", DbType.Int32, cityId);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        private static List<ShippingRate> GetEntitiesFromReader(IDataReader dataReader)
        {
            List<ShippingRate> shippingRateList = new List<ShippingRate>();
            while (dataReader.Read())
            {
                shippingRateList.Add(GetEntityFromReader(dataReader));
            }
            return shippingRateList;
        }

        private static ShippingRate GetEntityFromReader(IDataReader dataReader)
        {
            ShippingRate shippingRate = new ShippingRate();
            shippingRate.Id = (Int32)dataReader["Id"];
            shippingRate.CityId = (Int32)dataReader["CityId"];
            shippingRate.StartingWeight = (Double)dataReader["StartingWeight"];
            shippingRate.EndingWeight = (Double)dataReader["EndingWeight"];
            shippingRate.Rate = (Double)dataReader["Rate"];
            return shippingRate;
        }
    }
}

